package com.jy.service.filemanager.product;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jy.entity.filemanager.TSProduct;
import com.jy.repository.filemanager.ProductDao;
import com.jy.service.base.BaseServiceImp;
import com.jy.task.utils.TaskOperLogUtil;

@Service("productService")
public class ProductServiceImp extends BaseServiceImp<TSProduct> implements
		ProductService {

	/** 调度工厂Bean */
	@Autowired
	private Scheduler scheduler;

	@Override
	@Transactional
	public int creatProduct(TSProduct o) {
		int res = 0;
		try {
			o.setCreateDate(new Date());
			// 更新数据库
			super.insert(o);
			TaskOperLogUtil.saveTaskLog(1, "产品", "新增产品信息");
			res = 1;
		} catch (Exception e) {
			logger.error("创建任务失败", e);
		}
		return res;
	}

	@Override
	@Transactional
	public int updateProduct(TSProduct o) {
		int res = 0;
		try {
			ProductDao dao = (ProductDao) baseDao;
			// 从数据库查找原信息
			dao.update(o);
			TaskOperLogUtil.saveTaskLog(2, "产品", "修改产品信息");
			res = 1;
		} catch (Exception e) {
			logger.error("创建任务失败", e);
		}
		return res;
	}

	@Override
	@Transactional
	public int deleteProduct(TSProduct o) {
		int res = 0;
		try {
			ProductDao dao = (ProductDao) baseDao;
			// 从数据库查找原信息
			dao.deleteProduct(o);
			TaskOperLogUtil.saveTaskLog(3, "产品", "删除产品信息");
			res = 1;
		} catch (Exception e) {
			logger.error("删除任务失败", e);
		}
		return res;
	}

	@Override
	@Transactional
	public TSProduct getProductById(String id) {
		TSProduct objt = new TSProduct();
		try {
			ProductDao dao = (ProductDao) baseDao;
			// 从数据库查找原信息
			objt = dao.findProductByPId(id);
		} catch (Exception e) {
			logger.error("删除任务失败", e);
		}
		return objt;
	}

	@Override
	@Transactional
	public List<TSProduct> getProListByName(String name) {
		List<TSProduct> objList = new ArrayList<TSProduct>();
		try {
			ProductDao dao = (ProductDao) baseDao;
			// 从数据库查找原信息
			objList = dao.getProductListByName(name);
		} catch (Exception e) {
			logger.error("删除任务失败", e);
		}
		return objList;
	}
}
